# Author    :Dun_Hz
# Time      :2024/3/17 15:33
# 摇摆序列问题(Leetcode 376)
# 给定一个序列，求满足摇摆序列定义的最长子序列的长度
class Solution:
    def wiggleMaxLength(self, nums):
        if len(nums)<2:
            return len(nums)
        self.state = 0
        self.max_length = 1
        for i in range(1,len(nums)):
            if self.state==0:
                self.begin(i,nums)
            elif self.state==1:
                self.up(i,nums)
            elif self.state==2:
                self.down(i,nums)
        return self.max_length

    def begin(self,i,nums):
        if nums[i-1] > nums[i]:     #下降
            self.state = 2
            self.max_length +=1
        elif nums[i-1]< nums[i]:    #上升
            self.state=1
            self.max_length +=1

    def up(self,i,nums):
        if nums[i-1] > nums[i]:     #下降
            self.state = 2
            self.max_length +=1

    def down(self,i,nums):
        if nums[i-1] < nums[i]:     #下降
            self.state = 1
            self.max_length +=1

if __name__ =='__main__':
    S = Solution()
    g = [1,17,5,10,13,15,10,5,16,8]
    result = S.wiggleMaxLength(g)
    print(result)